$(function () {
    var form = window.form_query;
    var now = moment().format("YYYY-MM-DD");
    var timeLimitStart = form.timeLimitStart.value;
    var timeLimitEnd = form.timeLimitEnd.value;
    var startDate = moment(now).subtract(7, 'days').startOf('day'), 
    endDate = moment(now).endOf('day');
    if (timeLimitStart && timeLimitEnd) {
        startDate = moment(parseInt(timeLimitStart));
        endDate = moment(parseInt(timeLimitEnd));
    }

    $("#timelimitstart_and_timelimitstart").daterangepicker({
        "startDate": startDate,
        "endDate": endDate,
        "opens": "center",
        "autoApply": true,
        "ranges": {
            "今天": [moment(now).startOf('day'), moment(now).endOf('day')],
            "昨天": [moment(now).subtract(1, 'days').startOf('day'), moment(now).subtract(1, 'days').endOf('day')],
            "最近7天": [moment(now).subtract(7, 'days').startOf('day'), moment(now).endOf('day')],
            "最近30天": [moment(now).subtract(30, 'days').startOf('day'), moment(now).endOf('day')],
            "本月": [moment(now).startOf('month'), moment(now).endOf('month')],
            "上个月": [moment(now).subtract(1, 'months').startOf('month'), moment(now).subtract(1, 'months').endOf('month')]
        },
        "locale": {
            "firstDay": 1,
            "format": "YYYY-MM-DD",
            "separator": " 至 ",
            "daysOfWeek": ["日", "一", "二", "三", "四", "五", "六"],
            "monthNames": ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
            customRangeLabel: "自定义范围"
        }
    }, function (start, end, label) {
        form.timeLimitStart.value = start.startOf('day').format("x");
        form.timeLimitEnd.value = end.endOf('day').format("x");
    });
    $("#timelimitstart_and_timelimitstart").val('');//清空控件默认时间
    
    

  //支付弹框
  $('#md_edit_price').on('hide.bs.modal', function (event) {
      var form = window.form_edit_price;
      form.reset();
  });
  $('#md_edit_price').on('show.bs.modal', function (event) {
      var _e = $(event.relatedTarget);
      var product_id = _e.data("product_id");
      var proname = _e.data("proname");
      var procode = _e.data("procode");
      var modal = $(this);
      var form = window.form_edit_price;
      form.productid.value = product_id;
      form.productname.value = proname;
      form.productcode.value = procode;
  });

  //费用提交 直接进入支付界面
  $("[name='btn_save_price']").click(function () {
     var form = window.form_edit_price;
     if(form.price.value==''||form.price.value==null){
     	bootbox.balert("请输入每瓶支付的费用！");
     	return false ;
     }
     window.location.href="/payment/detail?price="+toDecimal2(form.price.value)+"&product_id="+form.productid.value+"&proname="+form.productname.value+"&procode="+form.productcode.value;
  });
  
  function toDecimal2(x) { 
      var f = parseFloat(x); 
      if (isNaN(f)) { 
        return false; 
      } 
      var f = Math.round(x*100)/100; 
      var s = f.toString(); 
      var rs = s.indexOf('.'); 
      if (rs < 0) { 
        rs = s.length; 
        s += '.'; 
      } 
      while (s.length <= rs + 2) { 
        s += '0'; 
      } 
      return s; 
    } 
});

//数据导出
$("[name='btn_export']").click(function () {
    var keyword = $("[name='keyword']").val();
    var timeLimitStart = $("[name='timeLimitStart']").val();
    var timeLimitEnd = $("[name='timeLimitEnd']").val();
   
    //有数据时才能导出
    if($("#resultTotal_hidden").val() != 0){
    	bootbox.bconfirm("确定是否导出？", function (re) {
            if (re) {
            	 var url="/payment/export?keyword="+keyword
            	 +"&timeLimitStart="+timeLimitStart+"&timeLimitEnd="+timeLimitEnd;
                 window.open(url);
            }
        });
    }else{
    	bootbox.balert("没有数据，无法导出!");
    }
});